//
//  SearchTableViewCell.m
//  GC_OC
//
//  Created by 曾 on 2022/1/17.
//

#import "SearchTableViewCell.h"

@implementation SearchTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(void)initView:(NSDictionary*)dic andIndex:(NSInteger)index andSearchStr:(NSString*)searchStr
{
    if(index==1)
    {
        
    }
//    @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
//    @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//    @property (weak, nonatomic) IBOutlet UILabel *contLabel;
//    @property (weak, nonatomic) IBOutlet UILabel *teachLabel;
//    @property (weak, nonatomic) IBOutlet UILabel *teachAllLabel;
//    @property (weak, nonatomic) IBOutlet UIView *lineView;
//    self.nameLabel.frame=CGRectMake(wid(15),15,SCREEN_WIDTH/2.0,wid(20));
//    self.nameLabel.text=@"圣经研究";
//    self.nameLabel.textColor=RBG(169,169,169);
//    self.nameLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(10)];
//    [self.nameLabel sizeToFit];
    self.nameLabel.frame=CGRectZero;
    CGFloat titleHeight=[GC_OCTool getHeightForLabel:@"A" andFont:[UIFont systemFontOfSize:FontSize(12)] andWidth:wid(100)];
    self.teachLabel.frame=CGRectMake(SCREEN_WIDTH/2.0,wid(20),SCREEN_WIDTH/2.0-wid(25),titleHeight);
    self.teachLabel.font=[UIFont systemFontOfSize:FontSize(12)];
    self.teachLabel.text=dic[@"teacher"];
    self.teachLabel.textColor=RBG(150,150,150);
    self.teachLabel.textAlignment=NSTextAlignmentRight;
    [self.teachLabel sizeToFit];
    self.teachLabel.frame=CGRectMake(SCREEN_WIDTH-self.teachLabel.frame.size.width-wid(25),wid(20),self.teachLabel.frame.size.width,titleHeight);
    if(self.teachLabel.frame.size.width>SCREEN_WIDTH/2.0-wid(25))
    {
        self.teachLabel.frame=CGRectMake(SCREEN_WIDTH/2.0,wid(65),SCREEN_WIDTH/2.0-wid(25),titleHeight);
    }

    self.titleLabel.frame=CGRectMake(wid(25),wid(20),SCREEN_WIDTH-self.teachLabel.frame.size.width-wid(50)-wid(20),titleHeight);
    self.titleLabel.font=[UIFont systemFontOfSize:FontSize(12)];
    self.titleLabel.text=dic[@"name"];
    self.titleLabel.textColor=[UIColor blackColor];

    
    self.contLabel.frame=CGRectMake(wid(25),wid(20)+self.titleLabel.frame.size.height+wid(10),SCREEN_WIDTH-wid(50),titleHeight);
    self.contLabel.font=[UIFont systemFontOfSize:FontSize(12)];
    if([dic[@"describe"] isEqual:[NSNull null]] || dic[@"describe"]==nil)
    {
        NSMutableAttributedString *attrDescribeStr = [[NSMutableAttributedString alloc] initWithString:@""];
        self.contLabel.attributedText=attrDescribeStr;
    }else
    {
        NSArray* array=[self calculateSubStringCount:dic[@"describe"] str:searchStr];
        NSMutableAttributedString *attrDescribeStr = [[NSMutableAttributedString alloc] initWithString:dic[@"describe"]];
        [attrDescribeStr addAttribute:NSForegroundColorAttributeName value:RBG(150,150,150)     range:NSMakeRange(0,attrDescribeStr.string.length)];
        for (int i=0;i<array.count;i++)
        {
            NSInteger index=[array[i] integerValue];
            [attrDescribeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor]  range:NSMakeRange(index,2)];
        }
        self.contLabel.attributedText=attrDescribeStr;
    }
    
//    self.teachAllLabel.frame=CGRectMake(SCREEN_WIDTH/2.0,wid(160),SCREEN_WIDTH/2.0-wid(25),titleHeight);
//    self.teachAllLabel.font=[UIFont systemFontOfSize:FontSize(12)];
//    self.teachAllLabel.text=@"李廷";
//    self.teachAllLabel.textColor=RBG(150,150,150);
//    self.teachAllLabel.textAlignment=NSTextAlignmentRight;
    self.teachAllLabel.frame=CGRectZero;
    CGFloat myHeig=self.contLabel.frame.origin.y+self.contLabel.frame.size.height+wid(20);
    self.lineView.frame=CGRectMake(wid(25),myHeig-1,SCREEN_WIDTH-wid(50),1);
    self.lineView.backgroundColor=RBG(240,240,240);
}

/**
 查找子字符串在父字符串中的所有位置
 @param content 父字符串
 @param tab 子字符串
 @return 返回位置数组
 */
- (NSMutableArray*)calculateSubStringCount:(NSString *)text str:(NSString *)findText
{
    NSMutableArray *arrayRanges = [NSMutableArray new];
       
       if (findText == nil && [findText isEqualToString:@""])
       {
           return @[];
       }
       NSRange rang = [text rangeOfString:findText]; //获取第一次出现的range
       
       if (rang.location != NSNotFound && rang.length != 0)
       {
           [arrayRanges addObject:[NSNumber numberWithInteger:rang.location]];//将第一次的加入到数组中
           NSRange rang1 = {0,0};
           NSInteger location = 0;
           NSInteger length = 0;
           for (int i = 0;;i++)
           {
               if (0 == i)
               {
                  //去掉这个abc字符串
                   location = rang.location + rang.length;
                   length = text.length - rang.location - rang.length;
                   rang1 = NSMakeRange(location, length);
               }
               else
               {
                   location = rang1.location + rang1.length;
                   length = text.length - rang1.location - rang1.length;
                   rang1 = NSMakeRange(location, length);
               }
               //在一个range范围内查找另一个字符串的range
               rang1 = [text rangeOfString:findText options:NSCaseInsensitiveSearch range:rang1];
               if (rang1.location == NSNotFound && rang1.length == 0)
               {
                   break;
               }
               else//添加符合条件的location进数组
               [arrayRanges addObject:[NSNumber numberWithInteger:rang1.location]];
           }
           return arrayRanges;
       }
       
       return @[];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
